package com.ymatou.iapi.sellercs.testcase;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.MessageResp;
import com.ymatou.iapi.sellercs.parameter.SellerSendMessageBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.parameter.SubmitScoreBean;
import com.ymatou.iapi.sellercs.service.SellerSendMessageCall;
import com.ymatou.iapi.sellercs.service.SubmitScoreCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
*提交评分
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SubmitScore{
	private static SubmitScoreBean submitscoreBean;
	private static SubmitScoreCall submitscoreCall;
	private static SellerSendMessageBean sellersendmessageBean;
	private static SellerSendMessageCall sellersendmessageCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("提交评分");
	}

	@Before
	public void caseUp() {
		submitscoreBean=new SubmitScoreBean();
		submitscoreCall=new SubmitScoreCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(submitscoreCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SubmitScore_001() {
		Logger.start(true,"提交评分,成功");
		try {
			//码头客服 sellerId 固定 测试用
			Integer kfid=SellerCSIMCallService.CsimId;
			Logger.comment("客服上线");
			CsimCallService cc = new CsimCallService();
			cc.getOnlineCS_Seller(null).getCSId();
			
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(kfid, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(2000);
			String msg=TestDataManager.getRandomNum(10);
			SellerCSIMCallService.SellerSendMessage(sellerId, kfid, msg);
			Thread.sleep(1000);
			
			cc.getAssignSellerUserCall();
			cc.getAssignBuyerUserCall();
			Thread.sleep(1000);
			//TODO 需要触发分配客服消息操作
			ArrayList<MessageResp> list = SellerCSIMCallService.GetMessageList(sellerId, kfid, true);
			Logger.debug(FormatUtil.beanToGSONString(list));
			String stssionnum=list.stream().filter(msgr->{return msgr.getMessage().equals(msg);}).collect(Collectors.maxBy(Comparator.comparingLong(MessageResp::getLongPostTime))).get().getSessionNumber();
			submitscoreBean.setAppId("auto");
			submitscoreBean.setScore(5);
			submitscoreBean.setUserId(sellerId);
			submitscoreBean.setSessionNumber(stssionnum);
			submitscoreCall.setData(submitscoreBean);
			submitscoreCall.callService();			
			Logger.verifyIsContains("200", submitscoreCall.getString("Status"), "验证Status");
			Logger.verifyEquals(true, submitscoreCall.getJsonObject().get("Result").getAsJsonObject().get("Success").getAsBoolean(), "验证Success");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SubmitScore_002() {
		Logger.start(true,"不输入UserId失败");
		try {
			submitscoreBean.setAppId("auto");
			submitscoreBean.setScore(5);
//			submitscoreBean.setUserId(sellerId);
			submitscoreBean.setSessionNumber("20181020120343_1097_24635724");
			submitscoreCall.setData(submitscoreBean);
			submitscoreCall.callService();			
			Logger.verifyIsContains("400", submitscoreCall.getString("Status"), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SubmitScore_003() {
		Logger.start(true,"不输入SessionNumber失败");
		try {
			submitscoreBean.setAppId("auto");
			submitscoreBean.setScore(5);
			submitscoreBean.setUserId(sellerId);
//			submitscoreBean.setSessionNumber("20181020120343_1097_24635724");
			submitscoreCall.setData(submitscoreBean);
			submitscoreCall.callService();	
			Logger.verifyIsContains("200", submitscoreCall.getString("Status"), "验证Status");
			Logger.verifyEquals(false, submitscoreCall.getJsonObject().get("Result").getAsJsonObject().get("Success").getAsBoolean(), "验证Success");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	@org.junit.Ignore
	//不考虑此场景-20181023 by 王旭东
	public void Tc_SubmitScore_004() {
		Logger.start(true,"不输入Score失败");
		try {
			submitscoreBean.setAppId("auto");
//			submitscoreBean.setScore(5);
			submitscoreBean.setUserId(sellerId);
			submitscoreBean.setSessionNumber("20181020120343_1097_24635724");
			submitscoreCall.setData(submitscoreBean);
			submitscoreCall.callService();	
			Logger.verifyIsContains("200", submitscoreCall.getString("Status"), "验证Status");
			Logger.verifyEquals(false, submitscoreCall.getJsonObject().get("Result").getAsJsonObject().get("Success").getAsBoolean(), "验证Success");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}